package com.ilove.media.model;

import java.io.IOException;
import javax.microedition.lcdui.*;
import org.kalmeo.kuix.core.model.DataProvider;
import com.ilove.model.*;

public class MediaDataProvider extends DataProvider {

	private String title;
	private String content;
	private String img;
	// 210.211.99.58:6868
	String vasUrl = "http://10h.vn:6868/AppServlet/servlet/SubIndex?";
	String cateUrl = "http://10h.vn:6868/AppServlet/servlet/Category?cateCode=";
	String contentlisturl = "http://10h.vn:6868/AppServlet/servlet/ContentList?cateCode=";
	String contenturl = "http://10h.vn:6868/AppServlet/servlet/Content?id=";
	String urlupload = "http://10h.vn:6868/Upload";
	private static final String CATEGORY_LISTS_PROPERTY = "categoryLists";
	private static final String TITLE_PROPERTY = "title";
	private static final String CONTENT_PROPERTY = "content";
	private static final String CONTENT_LISTS_PROPERTY = "contentlists";

	public void setImg(String img) {
		this.img = img;
		dispatchUpdateEvent("img");
	}
	public void setTitle(String title) {
		this.title = title;
		dispatchUpdateEvent(TITLE_PROPERTY);
	}

	public void setContent(String content) {
		this.content = content;
		dispatchUpdateEvent(CONTENT_PROPERTY);
	}
	// override the parent method to handle user defined value
	protected Object getUserDefinedValue(String property) {

		if (TITLE_PROPERTY.equals(property)) {
			return this.title;
		}

		if (CONTENT_PROPERTY.equals(property)) {
			return this.content;
		}

		if ("img".equals(property)) {
			return this.img;
		}

		// default behavior if the property has not been found
		return null;
	}
	protected boolean isActived() throws Exception {
		// ILOVE.KeyActive = RMSManager.readUID();
		// System.out.println("Start isActived");
		// String url = memberUrl + ILOVE.KeyActive;
		// System.out.println(url);
		// String sMember = getData(url);
		// if (sMember.startsWith("Active")) {
		// String UID = sMember.substring(10, sMember.length());
		// System.out.println("Active :" + UID);
		// RMSManager.saveUID(UID);
		// ILOVE.KeyActive = sMember;
		// Kuix.alert("Bạn cần kích hoạt gia hạn để tiếp tục
		// sử dụng ứng dụng. Bạn có đồng ý không? (Bạn có
		// thể sử dụng khi nhận được tin nhắn xác nhận kích
		// hoạt thành công)",
		// KuixConstants.ALERT_QUESTION
		// | KuixConstants.ALERT_YES
		// | KuixConstants.ALERT_NO, "sendsms",
		// "!exit");
		// } else {
		// System.out.println("UID :" + sMember);
		// RMSManager.saveUID(sMember);
		//
		// }
		return false;
	}
	/**
	 * get category list
	 */
	public String[] getCategoryList(String cateCode) throws Exception {

		String[] result = null;

		String url = cateUrl + cateCode;
		System.out.println(url);
		String sTopic = DataAccess.getData(url);
		if (!"".equalsIgnoreCase(sTopic.trim()))
			result = DataAccess.split(sTopic, "\n");

		return result;

	}
	public int getTotalCategory(String cateCode) throws Exception {

		int total = 1;
		String url = cateUrl + cateCode + "&total=1";
		System.out.println(url);
		String sTopic = DataAccess.getData(url);
		if(!"".equalsIgnoreCase(sTopic))
		{
			total = Integer.parseInt(sTopic.trim());
			System.out.println(total);
		}
		return total;
	}
	public String[] getCategoryListWithPage(String cateCode,int currPage,int pageSize) throws Exception {

		String[] result = null;

		String url = cateUrl + cateCode+"&page="+currPage+"&pagesize="+pageSize;
		System.out.println(url);
		String sTopic = DataAccess.getData(url);
		if (!"".equalsIgnoreCase(sTopic.trim()))
			result = DataAccess.split(sTopic, "\n");

		return result;

	}
	
	public void setCategoryList(String[] list) {
		removeAllItems(CATEGORY_LISTS_PROPERTY);
		for (int i = 0; i < list.length - 1; i++) {
			int id = 0;
			String code = "";
			String name = "";
			String data = list[i];
			id = i + 1;
			code = data.substring(0, data.indexOf("@@@"));
			name = data.substring(data.indexOf("@@@") + 3, data.length() - 1);
			CategoryList categorylist = new CategoryList();
			categorylist.id = id;
			categorylist.categoryCode = code;
			categorylist.categoryName = name;
			addItem(CATEGORY_LISTS_PROPERTY, categorylist);
		}
	}
	public String[] getContentList(String catecode,int currPage,int pageSize) throws Exception {

		String url = contentlisturl + catecode+"&page="+currPage+"&pagesize="+pageSize;;
		System.out.println("getContentList" + url);
		String sContent = DataAccess.getData(url);
		if ("".equalsIgnoreCase(sContent.trim()))
			return null;
		String[] subIndex = DataAccess.split(sContent, "\n");
		return subIndex;
	}
	public int getTotalContent(String cateCode) throws Exception {

		int total = 1;
		String url = contentlisturl + cateCode + "&total=1";
		System.out.println(url);
		String sTopic = DataAccess.getData(url);
		if(!"".equalsIgnoreCase(sTopic))
		{
			total = Integer.parseInt(sTopic.trim());
			System.out.println(total);
		}
		return total;
	}
	public void setContentList(String[] list) {
		removeAllItems(CONTENT_LISTS_PROPERTY);
		Image imgav = null;
		for (int i = 0; i < list.length - 1; i++) {
			String data = list[i];
			System.out.println(data);
			String[] content = DataAccess.split(list[i], "@@@");
			ContentList cl = new ContentList();
			cl.code = content[0];
			cl.name = content[1];
			cl.img = content[2];

			if (!"".equalsIgnoreCase(content[2].trim())) {
				String urlimage = urlupload
						+ content[2];

				try {
					imgav = DataAccess.getImage(urlimage.trim());
				} catch (IOException e) {
					e.printStackTrace();
				}
				cl.avatar = imgav;
			}
			addItem(CONTENT_LISTS_PROPERTY, cl);
		}
	}
	public ContentList getContentData(String contentid, int type)
			throws Exception {
		// Get content from server and split into array
		removeAllItems(CONTENT_PROPERTY);
		String url = "";
		if (type != 0)
			url = contenturl + contentid + "&type=" + type;
		else
			url = vasUrl + "id=" + contentid;

		System.out.println("getContentData" + url);
		String sContent = DataAccess.getData(url);
		String[] content = null;
		if ("null".equalsIgnoreCase(sContent)
				|| "".equalsIgnoreCase(sContent.trim()))
			return null;
		else
				content = DataAccess.split(sContent, "@@@");

		ContentList list = new ContentList();
		list.code = content[0];		// Name content
		list.name = content[1];		// Data content , url image url ringtone, detail game
		list.img = content[2];		// url avatar

		return list;
	}
}